//
//  SideSlipPriceTableViewCell.m
//  quxiangtao
//
//  Created by xbmios on 2018/8/7.
//  Copyright © 2018年 shanhaijin. All rights reserved.
//

#import "SideSlipPriceTableViewCell.h"

#define TEXTFIELD_MAX_LENGTH 6

#define ACCESSORY_VIEW_HEIGHT 34
#define ACCESSORY_BUTTON_WIDTH 50
#define ACCESSORY_BUTTON_LEADING_TRAILING 0

@interface SideSlipPriceTableViewCell ()<UITextFieldDelegate>

@property (strong, nonatomic) UILabel *titleL;

@property (strong, nonatomic) UITextField *lowPriceTextField;
@property (strong, nonatomic) UITextField *highPriceTextField;
@property (strong, nonatomic) UIView *lineV;

@property (strong, nonatomic) ZYSideSlipFilterRegionModel *regionModel;

@end

@implementation SideSlipPriceTableViewCell
+ (NSString *)cellReuseIdentifier {
    return @"SideSlipPriceTableViewCell";
}

+ (instancetype)createCellWithIndexPath:(NSIndexPath *)indexPath {

    SideSlipPriceTableViewCell *cell = [[NSClassFromString(@"SideSlipPriceTableViewCell") alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SideSlipPriceTableViewCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.lowPriceTextField.delegate = cell;
    cell.highPriceTextField.delegate = cell;
    [cell configureKeyboard];
    return cell;
}
- (void)configureKeyboard {
    UIView *keyBoardAccessoryView = [self createKeyBoardAccessoryView];
    self.lowPriceTextField.inputAccessoryView = keyBoardAccessoryView;
    self.highPriceTextField.inputAccessoryView = keyBoardAccessoryView;
}

- (void)updateCellWithModel:(ZYSideSlipFilterRegionModel *__autoreleasing *)model
                  indexPath:(NSIndexPath *)indexPath {
    self.regionModel = *model;
    
    if (self.cellHeigthBlock) {
        
        self.cellHeigthBlock(jisuan(164));
    }
    
}
- (void)resetData {
    [self.lowPriceTextField setText:@""];
    [self.highPriceTextField setText:@""];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (textField.text.length >= TEXTFIELD_MAX_LENGTH && ![string isEqualToString:@""]) {
        return NO;
    }
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
//    PriceRangeModel *model = [[PriceRangeModel alloc] init];
//    model.minPrice = [_minTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
//    model.maxPrice = [_maxTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
//    NSMutableDictionary *mutDict = [NSMutableDictionary dictionaryWithDictionary:_regionModel.customDict];
//    [mutDict setValue:model forKey:PRICE_RANGE_MODEL];
//    _regionModel.customDict = [mutDict copy];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.lowPriceTextField resignFirstResponder];
    [self.highPriceTextField resignFirstResponder];
}

- (void)accessoryButtonBack {
    if ([self.lowPriceTextField isFirstResponder]) {
        [self.lowPriceTextField setText:@""];
        [self.lowPriceTextField resignFirstResponder];
    } else if ([self.highPriceTextField isFirstResponder]) {
        [self.highPriceTextField setText:@""];
        [self.highPriceTextField resignFirstResponder];
    }
}

- (void)accessoryButtonDone {
    [self.lowPriceTextField resignFirstResponder];
    [self.highPriceTextField resignFirstResponder];
}


- (UIView *)createKeyBoardAccessoryView {
    UIView *keyBoardAccessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ACCESSORY_VIEW_HEIGHT)];
    [keyBoardAccessoryView setBackgroundColor:Colorefefef];
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(ACCESSORY_BUTTON_LEADING_TRAILING, 0, ACCESSORY_BUTTON_WIDTH, ACCESSORY_VIEW_HEIGHT)];
    [backButton setTitle:@"取消" forState:UIControlStateNormal];
    [backButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [backButton.titleLabel setFont:[UIFont systemFontOfSize:14.f]];
    [backButton addTarget:self action:@selector(accessoryButtonBack) forControlEvents:UIControlEventTouchUpInside];
    UIButton *doneButton = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - ACCESSORY_BUTTON_LEADING_TRAILING - ACCESSORY_BUTTON_WIDTH, 0, ACCESSORY_BUTTON_WIDTH, ACCESSORY_VIEW_HEIGHT)];
    [doneButton setTitle:@"完成" forState:UIControlStateNormal];
    [doneButton setTitleColor:Colorff0000 forState:UIControlStateNormal];
    [doneButton.titleLabel setFont:[UIFont systemFontOfSize:14.f]];
    [doneButton addTarget:self action:@selector(accessoryButtonDone) forControlEvents:UIControlEventTouchUpInside];
    [keyBoardAccessoryView addSubview:backButton];
    [keyBoardAccessoryView addSubview:doneButton];
    return keyBoardAccessoryView;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {

        [self.contentView addSubview:self.titleL];
        [self.contentView addSubview:self.lowPriceTextField];
        [self.contentView addSubview:self.lineV];
        [self.contentView addSubview:self.highPriceTextField];

        [self createUI];
    }

    return self;

}

- (void)createUI {

    __weak typeof(self) weakSelf = self;
    
    [self.titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.contentView.mas_left).with.offset(jisuan(20));
        make.top.mas_equalTo(weakSelf.contentView.mas_top).with.offset(jisuan(30));
    }];
    [self.lowPriceTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.contentView.mas_left).with.offset(jisuan(20));
        make.top.mas_equalTo(weakSelf.titleL.mas_bottom).with.offset(jisuan(30));
        make.height.mas_equalTo(jisuan(60));
        make.width.mas_equalTo(jisuan(290));
    }];
    
    [self.highPriceTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(weakSelf.contentView.mas_right).with.offset(-jisuan(20));
        make.top.mas_equalTo(weakSelf.titleL.mas_bottom).with.offset(jisuan(30));
        make.height.mas_equalTo(jisuan(60));
        make.width.mas_equalTo(jisuan(290));
    }];
    
    [self.lineV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(weakSelf.contentView.mas_centerX);
        make.centerY.mas_equalTo(weakSelf.lowPriceTextField.mas_centerY);

        make.height.mas_equalTo(jisuan(2));
        make.width.mas_equalTo(jisuan(16));
    }];
}


#pragma mark -lazy
- (UILabel *)titleL {
    
    if (!_titleL) {
        
        _titleL = [[UILabel alloc] init];
        _titleL.font = Font(24);
        _titleL.textColor = Color333333;
        _titleL.textAlignment = NSTextAlignmentLeft;
        _titleL.text = @"价格";
        
    }
    return _titleL;
    
}
- (UITextField *)lowPriceTextField {
    
    if (!_lowPriceTextField) {
        
        _lowPriceTextField = [[UITextField alloc] init];
        _lowPriceTextField.textAlignment = NSTextAlignmentCenter;
        _lowPriceTextField.textColor = Color333333;
        _lowPriceTextField.placeholder = @"最低价";
        _lowPriceTextField.font = Font(24);
        _lowPriceTextField.keyboardType = UIKeyboardTypeNumberPad;

        _lowPriceTextField.backgroundColor = Colorefefef;
        _lowPriceTextField.layer.cornerRadius = jisuan(10);
        _lowPriceTextField.layer.masksToBounds = YES;
    }
    
    return _lowPriceTextField;
}

- (UITextField *)highPriceTextField {
    
    if (!_highPriceTextField) {
        
        _highPriceTextField = [[UITextField alloc] init];
        _highPriceTextField.textAlignment = NSTextAlignmentCenter;
        _highPriceTextField.textColor = Color333333;
        _highPriceTextField.placeholder = @"最高价";
        _highPriceTextField.font = Font(24);
        _highPriceTextField.keyboardType = UIKeyboardTypeNumberPad;
        
        _highPriceTextField.backgroundColor = Colorefefef;
        _highPriceTextField.layer.cornerRadius = jisuan(10);
        _highPriceTextField.layer.masksToBounds = YES;
        
    }
    
    return _highPriceTextField;
}
- (UIView *)lineV {
    
    if (!_lineV) {
        
        _lineV = [[UIView alloc] init];
        _lineV.backgroundColor = Colordddddd;
    }
    
    return _lineV;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
